package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.SetMealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealService setmealService;

    @Override
    public Boolean saveSetMeal(SetmealDTO setmealDTO) {
        //看下前端要有没有传过来id值
        Integer setmealDTOId = setmealDTO.getId();
        //如果前端传过来id值，就先把中间表通过id进行删除
        if (StringUtils.isNotBlank(String.valueOf(setmealDTOId))){
            baseMapper.deleteCheckGroupIdsBySetMealId(setmealDTOId);
        }
        boolean flag = this.saveOrUpdate(setmealDTO);
        Integer setmealId = setmealDTO.getId();
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        if (checkgroupIds!=null && checkgroupIds.length!=0){

            for (Integer checkgroupId : checkgroupIds) {
                baseMapper.addSetMealIdAndCheckGroupId(setmealId, checkgroupId);
            }

        }
        return flag;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        Page<Setmeal> page = null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())){
            page = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }else {
            wrapper.and(i->i.like("code",queryPageBean.getQueryString()));
            wrapper.or().like("name",queryPageBean.getQueryString());
            wrapper.or().like("helpcode",queryPageBean.getQueryString());
            page = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), wrapper);
        }

        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public SetmealVO findSetMealDetail(int id) {

        SetmealVO setmealVO = baseMapper.selectSetMealById(id);
        List<CheckGroupVO> checkGroupList = baseMapper.findCheckGroupsById(id);

        for (CheckGroupVO checkGroupVO : checkGroupList) {
            List<CheckItem> checkItemList = baseMapper.findCheckItemsByGroupId(checkGroupVO.getId());
            checkGroupVO.setCheckItemList(checkItemList);
        }
        setmealVO.setCheckGroups(checkGroupList);
        return setmealVO;
    }

    @Override
    public void clearOssObject() {
        Set<String> members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if (members.size()!=0){
            for (String fileName : members) {
                Boolean flag = RedisUtil.existsKey(RedisConstant.SINGLE_PIC + fileName);
                //表示此照片信息在redis中已经过期
                if (flag==false){
                    AliyunUtils.deleteFile(fileName);
                    System.out.println("---阿里云删除垃圾图片一次---");
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,fileName);
                }
            }
        }
    }

    @Override
    public Setmeal findSetMailById(int id) {
        return baseMapper.findSetMailById(id);
    }


}
